// -*- c++ -*-

#ifndef SCEOSTHREADMANAGER_H__
#define SCEOSTHREADMANAGER_H__

#include <string>

#ifdef WIN32
#pragma warning (disable : 4290)
#endif

/*
 * Abstract:
 *
 *     ThreadManager is used to start and manage threads.
 */

namespace SCE {
  namespace OS {

    class ThreadManager {
    public:
      /*
       * Constructor: Initializes the instance, but does not start a thread.
       */

      ThreadManager ()
	throw ();

      /*
       * Constructor: Initializes the instance, the calls
       *              start (func, opaque).
       */

      ThreadManager (void (*func) (void *), void * opaque)
	throw (std::string);

      /*
       * Destructor.
       *
       * The instance may only be destructed if it is not managing a
       * thread. If a thread was started, join() or detach() must be
       * called first.
       */

      ~ThreadManager ()
	throw ();

      /*
       * start(): Starts a thread, and manages it.
       *
       * [in] func:   The function to run in the new thread. It is passed
       *              "opaque" as it single parameter. The thread ends
       *              when the function returns.
       * [in] opaque: A parameter to pass to "func" in the new thread.
       *
       * May not be called if this instance already manages a thread.
       * May be called again after calling join() or detach().
       */

      void start (void (*func) (void *), void * opaque)
	throw (std::string);

      /*
       * join(): Waits for the completion of a thread.
       *
       * Blocks until the thread's "func" function returns.
       */

      void join ()
	throw (std::string);

      /*
       * detach(): Cease management of the managed thread.
       *
       * "Forgets" about the started thread, which will continue to
       * run.
       */

      void detach ()
	throw (std::string);

    private:
      char m_osOpaque[8];

    private:
      ThreadManager (const ThreadManager &);
      ThreadManager & operator= (const ThreadManager &);
    };

  }
}

#endif
